<?php
class SourceDataReportForm extends sfFormSymfony
{
  public function configure()
  {
    $this->setWidget('plant', new sfWidgetFormDoctrineChoice(array(
                                        'model'=>'Plant', 
                                        'add_empty'=>True, 
                                        'query' => Doctrine_Query::create()->from('Plant p')->select('id, name')->where('enable=1'),
                                        'default' => $this->getOption("user")->getAttribute("quest_plant", false)))
                            );
    
    $this->setValidator('plant',new sfValidatorDoctrineChoice(array(
                                        'model'=>'Plant', 
                                        'multiple'=>true,
                                        'query' => Doctrine_Query::create()->from('Plant p')->select('id, name')->where('1'),
                                        )));
    
    if ($this->getOption("user")->getAttribute("quest_plant", false)){
        $plant= Doctrine_Core::getTable('Plant')->find($this->getOption("user")->getAttribute("quest_plant"));

        $this->setWidget('powerdev', new sfWidgetFormDoctrineChoice(array(
                                                'model'=>'Powerdev',
                                                'add_empty'=>True,
                                                'multiple'=>False,
                                                'query' => Doctrine_Query::create()->from('Powerdev p')->select('id, plant, type, label')->andwhere('plant='.$plant->getId())->andWhere('p.address IS NOT NULL or p.address2 IS NOT NULL'),
                                                'default' => $this->getOption("user")->getAttribute("quest_powerdev", false)))
                );
        $this->setValidator('powerdev',new sfValidatorDoctrineChoice(array(
                                        'model'=>'Powerdev', 
                                        'multiple'=>true,
                                        'query' => Doctrine_Query::create()->from('Powerdev p')->select('id, plant, type, label')->andwhere('plant='.$plant->getId())->andWhere('p.address IS NOT NULL or p.address2 IS NOT NULL'),
                                        )));
        
        $this->widgetSchema->setHelp("powerdev","Select the device");

        
        
        if ($this->getOption("user")->getAttribute("quest_powerdev", false)){

            $powerdev=  Doctrine_Core::getTable('Powerdev')->find($this->getOption("user")->getAttribute("quest_powerdev", false));
            $monVars=sfConfig::get("app_monitored_var");

            
            foreach($monVars[$powerdev->getType()] as $var){
                if (strcmp("Sampletime",$var)<>0)
                    $devVars[$plant->getName()."-->".$powerdev->getLabel()."-->".$var]=$plant->getName()."-->".$powerdev->getLabel()."-->".$var;
            }
            
            if ($this->getOption("user")->hasAttribute("validVar"))
                $this->getOption("user")->setAttribute("validVar", array_merge($devVars,$this->getOption("user")->getAttribute("validVar", NULL)));
            else
                $this->getOption("user")->setAttribute("validVar", $devVars);
           

            $w=new sfWidgetFormChoice(array('choices'=> $devVars,
                                            'multiple'=>true));
            
            $this->setWidget('variables', $w);
            
            $this->setValidator('variables', new sfValidatorChoice(array(
                                            'choices'=> array_keys($this->getOption("user")->getAttribute("validVar")),
                                            'multiple'=>true
                                        )));
            
             $this->widgetSchema->setHelp("variables","Select one or more variables. <br> Change device to add more variables.");

            
            $this->setWidget('date', new sfWidgetFormDateRange(array(
                'from_date' => new sfWidgetFormDate(array('format'=>'%day%%month%%year%')),
                'to_date' => new sfWidgetFormDate(array('format'=>'%day%%month%%year%'))
            )));
            
            $this->setValidator('date', new sfValidatorDateRange(array(
                                                'required'=>true,
                                                'from_date' => new sfValidatorDate(array('required' => true,'datetime_output' => 'd-m-Y')),
                                                'to_date'   => new sfValidatorDate(array('required' => true,'datetime_output' => 'd-m-Y')),
                                        )));
            $this->widgetSchema->setHelp("date","Select dates range");

       
        } 
    }
    
    $this->widgetSchema->setHelp("plant","Select the plant");
    $this->getWidgetSchema()->setNameFormat('srcData[%s]');
  }  
}

